/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <Winsock2.h>
#include <Ws2tcpip.h>
#include <Iphlpapi.h>

class MIpHelper
{
private:
	CString xAdapterName;
	CString xDescription;
	UINT    xHwAddrLen;
	BYTE    xHwAddr[MAX_ADAPTER_ADDRESS_LENGTH];
	UINT    xType;
	bool    xDhcpEnabled;
	bool    xHaveWins;
	CString xIpAddr;
	CString xIpMask;
	CString xIpAddrGw;
	CString xIpMaskGw;
	CString xIpAddrDhcp;
	CString xIpMaskDhcp;
	CString xIpAddrWins1;
	CString xIpMaskWins1;
	CString xIpAddrWins2;
	CString xIpMaskWins2;
	time_t  xLeaseObtained;
	time_t  xLeaseExpires;
	DWORD   xAdapterIndex;

	/// In fact, there are many such addresses but I am gathering only the first one
	sockaddr_in6 xIPv6Unicast;
	sockaddr_in6 xIPv6Anycast;
	sockaddr_in6 xIPv6Multicast;
	sockaddr_in6 xIPv6DNS;

	CString xHostName;
	CString xDomainName;
	CString xDnsServer;

	void Initialize(void);
	void LoadFromAdapterInfo(IP_ADAPTER_INFO*);

	bool GetNetworkParameters(void);

	void GetIPv6Address(void);

public:
	MIpHelper(void);
	~MIpHelper(void);

	bool FindAdapter(CString);

	BYTE macAddress[6];
	in_addr ipAddress;
	in_addr ipMask;

	in_addr ipAddrGw;

	BYTE macDnsServer[6];
	in_addr ipAddrDns;

	bool IpAddrBelongsToLocalNet(DWORD);

	void GetIPv6UnicastAddress(in_addr6*);
	DWORD GetIPv6UnicastAddressScope(void);
	void GetIPv6AnycastAddress(in_addr6*);
	void GetIPv6MulticastAddress(in_addr6*);
	void GetIPv6DNSAddress(in_addr6*);

	bool FlushArpTable(void);
	bool AddArpTableEntry(DWORD, BYTE*);
};
